package com.ming.attendance.service.impl;

import com.ming.attendance.mapper.AttendanceEventMapper;
import com.ming.attendance.mapper.EventUserMapper;
import com.ming.attendance.mapper.WxUserMapper;
import com.ming.attendance.po.AttendanceEvent;
import com.ming.attendance.po.EventUser;
import com.ming.attendance.po.ResponseBean;
import com.ming.attendance.po.WxUser;
import com.ming.attendance.service.ExportReportService;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ExportReportServiceImpl implements ExportReportService {
    @Autowired
    private WxUserMapper wum;
    @Autowired
    private EventUserMapper eum;
    @Autowired
    private AttendanceEventMapper aem;

    @Override
    public XSSFWorkbook getReportByExcel(String eventid) {
        // 先从数据库查出数据
        AttendanceEvent event = aem.getEvent(eventid);
        // 创建工作簿
        XSSFWorkbook wb = new XSSFWorkbook();
        XSSFSheet sheet = wb.createSheet(event.getTitle()); // 创建表
        // 设置列宽
        sheet.setColumnWidth(1,20*256);
        sheet.setColumnWidth(2,20*256);
        sheet.setColumnWidth(3,20*256);

        XSSFRow titleRow = sheet.createRow(0);  // 为第一行添加表头
        titleRow.createCell(0).setCellValue("用户昵称");

        titleRow.createCell(1).setCellValue("加入打卡事件时间");
        titleRow.createCell(2).setCellValue("打卡时间");
        titleRow.createCell(3).setCellValue("上次登录时间");
        titleRow.createCell(4).setCellValue("是否打卡（1：打卡/0：未打卡）");
        // 创建第二张表 将事件相关信息输出到其中
        // XSSFSheet sheet2 = wb.createSheet(event.getTitle() + "_事件相关信息表");
        // sheet2.createRow(0);



        // 初始化特殊单元格的样式
        XSSFCellStyle style = wb.createCellStyle();
        // XSSFColor color = new XSSFColor();
        // color.setRGB(hexStringToByteArray("ffff00"));
        style.setFillBackgroundColor(new HSSFColor.YELLOW().getIndex());    // ### 颜色未生效 ###

        // 将数据装填进表中
        int rowNum = 1;
        for (EventUser eventUser : event.getListEventUser()) {
            XSSFRow row = sheet.createRow(rowNum);
            WxUser user = eventUser.getUser();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            row.createCell(0).setCellValue(user.getNickName());
            row.createCell(1).setCellValue(dateFormat.format(new Date(Long.parseLong(eventUser.getJoinTime()))));
            if(eventUser.getIsChecked()==1)
                row.createCell(2).setCellValue(dateFormat.format(new Date(Long.parseLong(eventUser.getCheckTime()))));
            else
                row.createCell(2);
            row.createCell(3).setCellValue(dateFormat.format(new Date(Long.parseLong(user.getLastTime()))));
            row.createCell(4).setCellValue(eventUser.getIsChecked());

            row.setRowStyle(style); // 添加样式

            rowNum++;
        }

        return wb;
    }



    /**
     * 16进制表示的字符串转换为字节数组
     *
     * @param hexString 16进制表示的字符串
     * @return byte[] 字节数组
     */
    public static byte[] hexStringToByteArray(String hexString) {
        hexString = hexString.replaceAll(" ", "");
        int len = hexString.length();
        byte[] bytes = new byte[len / 2];
        for (int i = 0; i < len; i += 2) {
            // 两位一组，表示一个字节,把这样表示的16进制字符串，还原成一个字节
            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
                    .digit(hexString.charAt(i + 1), 16));
        }
        return bytes;
    }
}
